package com.ruoyi.kjcg.qygfpw.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.kjcg.qygfpw.domain.Qygfpw;
import com.ruoyi.kjcg.qygfpw.domain.QygfpwFj;
import com.ruoyi.kjcg.qygfpw.service.IQygfpwService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 企业工法批文Controller
 *
 * @author huacai
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/kjcg/qygfpw")
public class QygfpwController extends BaseController {
    @Resource
    private IQygfpwService qygfpwService;

    /**
     * 查询企业工法批文列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfpw:list')")
    @GetMapping("/list")
    public TableDataInfo list(Qygfpw qygfpw) {
        startPage();
        List<Qygfpw> list = qygfpwService.selectQygfpwList(qygfpw);
        return getDataTable(list);
    }

    /**
     * 导出企业工法批文列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfpw:export')")
    @Log(title = "企业工法批文", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Qygfpw qygfpw) {
        List<Qygfpw> list = qygfpwService.selectQygfpwList(qygfpw);
        ExcelUtil<Qygfpw> util = new ExcelUtil<Qygfpw>(Qygfpw.class);
        util.exportExcel(response, list, "企业工法批文数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Qygfpw> util = new ExcelUtil<Qygfpw>(Qygfpw.class);
        util.importTemplateExcel(response, "企业工法批文数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "企业工法批文", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjcg:qygfpw:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Qygfpw> util = new ExcelUtil<Qygfpw>(Qygfpw.class);
        InputStream inputStream = file.getInputStream();
        List<Qygfpw> list = util.importExcel(inputStream);
        inputStream.close();
        int count = qygfpwService.batchInsertQygfpw(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取企业工法批文详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfpw:query')")
    @GetMapping(value = "/{qygfpwId}")
    public AjaxResult getInfo(@PathVariable("qygfpwId") String qygfpwId) {
        return success(qygfpwService.selectQygfpwByQygfpwId(qygfpwId));
    }

    /**
     * 新增企业工法批文
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfpw:add')")
    @Log(title = "企业工法批文", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Qygfpw qygfpw) {
        qygfpw.setUserId(Math.toIntExact(getUserId())); //用户ID
        qygfpw.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        qygfpw.setCreateBy(getUsername()); //创建人
        qygfpw.setIdWhere("存在");
        return toAjax(qygfpwService.insertQygfpw(qygfpw));
    }

    /**
     * 修改企业工法批文
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfpw:edit')")
    @Log(title = "企业工法批文", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Qygfpw qygfpw) {
        return toAjax(qygfpwService.updateQygfpw(qygfpw));
    }

    /**
     * 删除企业工法批文
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfpw:remove')")
    @Log(title = "企业工法批文", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qygfpwIds}")
    public AjaxResult remove(@PathVariable String[] qygfpwIds) {
        return toAjax(qygfpwService.deleteQygfpwByQygfpwIds(qygfpwIds));
    }

    /**
     * 获得企业工法批文主键ID (UUID)
     */
    @GetMapping("/getQyGfPwId")
    public AjaxResult getQyGfPwId(){
        String getQyGfPwId = IdUtils.fastSimpleUUID();
        return success(getQyGfPwId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertQygfpwFjByQygfpwId(@RequestBody QygfpwFj qygfpwFj){
        return toAjax(qygfpwService.insertQygfpwFjByQygfpwId(qygfpwFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectQygfpwFjList(String sId){
        QygfpwFj qygfpwFj = new QygfpwFj();
        qygfpwFj.setQygfpwId(sId);
        return AjaxResult.success(qygfpwService.selectQygfpwFjList(qygfpwFj));
    }

    /**
     * 删除企业工法批文附件
     */
    @Log(title = "企业工法批文附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(qygfpwService.deleteQygfpwFjByFjIds(fjIds));
    }
}
